package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
	@Autowired
	private DishFlavorService dishFlavorService;
	@Autowired
	private SetmealDishService setmealDishService;


	@Override
	@Transactional
	public void saveDishAndDishFlavor(DishDto dishDto) {
		//1.保存到Dish表
		// System.out.println(dishDto.getId());
		super.save(dishDto);
		// System.out.println(dishDto.getId());

		//2.保存到dish_flavor表
		Long dishId = dishDto.getId();
		List<DishFlavor> flavorList = dishDto.getFlavors().stream().map((dishFlavor -> {
			dishFlavor.setDishId(dishId);
			return dishFlavor;
		})).collect(Collectors.toList());

		dishFlavorService.saveBatch(flavorList);

	}
	@Override
	@Transactional
	public void updateWithFlavor(DishDto dishDto) {
		//1.更新dish表基本信息
		this.updateById(dishDto);

		//2.清理当前菜品对应口味数据---dish_flavor表的delete操作
		LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
		queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());

		dishFlavorService.remove(queryWrapper);

		//3.添加当前提交过来的口味数据---dish_flavor表的insert操作
		List<DishFlavor> flavors = dishDto.getFlavors();

		flavors = flavors.stream().map((item) -> {
			item.setDishId(dishDto.getId());
			return item;
		}).collect(Collectors.toList());
		//4.批量保存
		dishFlavorService.saveBatch(flavors);
	}

	/**
	 * 删除菜品，同时需要删除菜品和对应的口味的关联数据
	 * @param ids
	 */
	@Transactional
	@Override
	public void removeWithFlavor(List<Long> ids) {
		//select count(*) from dish where id in (1,2,3) and status = 1
		//查询菜品状态，确定是否可用删除
		LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.in(Dish::getId,ids);
		queryWrapper.eq(Dish::getStatus,1);

		int count = super.count(queryWrapper);
		if (count > 0){
			//如果不能删除，抛出一个业务异常
			throw new CustomException("菜品正在售卖中，不能删除");
		}

		//查询菜品是否被包含在套餐中，确认是否可删除
		LambdaQueryWrapper<SetmealDish> setmealDishQueryWrapper = new
				LambdaQueryWrapper<>();
		setmealDishQueryWrapper.in(SetmealDish::getDishId,ids);
		SetmealDish one = setmealDishService.getOne(setmealDishQueryWrapper);

		if (one != null){
			//如果不能删除，抛出一个业务异常
			throw new CustomException("菜品包含在某个套餐中，不能删除");
		}

		//如果可以删除，先删除菜品表中的数据---dish
		super.removeByIds(ids);

		//再查询口味表中对应菜品id的口味信息并删除
		LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.in(DishFlavor::getDishId,ids);
		//删除关系表中的数据---dish_flavor
		dishFlavorService.remove(lambdaQueryWrapper);
	}

	@Override
	public List<DishDto> getDishDtoSByCategoryId(Dish dish) {
		//1.查询Dish集合
		LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(Dish::getCategoryId,dish.getCategoryId());
		queryWrapper.eq(Dish::getStatus,1);//在售商品才查询
		List<Dish> dishes = super.list(queryWrapper);

		//2.把dish集合装成DishDto集合
		List<DishDto> dishDtoList = dishes.stream().map(dish1 -> {
			DishDto dishDto = new DishDto();
			BeanUtils.copyProperties(dish1,dishDto);
			//查询口味集合select * from dish_flavers where dish_id = ?
			LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
			queryWrapper1.eq(DishFlavor::getDishId,dish1.getId());
			List<DishFlavor> dishFlavors = dishFlavorService.list(queryWrapper1);
			//在dishDto中设置flavors的属性值
			dishDto.setFlavors(dishFlavors);
			return dishDto;
		}).collect(Collectors.toList());
		return dishDtoList;
	}
}
